package com.controller;

import com.entity.Air;
import com.entity.District;
import com.service.Airservice;
import com.service.Districtservice;
import com.tools.PageSupport;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018/12/11.
 */
@Controller
public class Indexcontroller {
    @Resource
    private Districtservice districtservice;
    @Resource
    private Airservice airservice;
    private PageSupport pageSupport=new PageSupport();
    @RequestMapping(value = "/district",method = RequestMethod.GET)
    public String district(Model model,@RequestParam(name="queryDistrict",required = false) String queryDistrict,@RequestParam(required = false) Integer pageIndex){
        if(pageIndex==null){
            pageIndex=1;
        }
        Integer id = 0;
        if(null!=queryDistrict){
            id = Integer.parseInt(queryDistrict);
        }
        List<Air> airList = airservice.getAir(id,pageIndex);
        List<District> districtList = districtservice.getDistrict();
        model.addAttribute("districtList",districtList);
        model.addAttribute("airList",airList);
        model.addAttribute("pageIndex",pageIndex);
        int counts=airservice.getCounts(id);
        pageSupport.setTotalCount(counts);
        model.addAttribute("pages",pageSupport.getTotalPageCount());
        return "index";
    }
    @RequestMapping(value = "/addair",method = RequestMethod.GET)
    public String addair(Model model){
        try {
            List<District> districtList = districtservice.getDistrict();
            model.addAttribute("districtList", districtList);
        }catch (Exception e){
            throw new RuntimeException("添加失败!");
        }
        return "addair";
    }
    @RequestMapping(value = "/adddistrict",method = RequestMethod.GET)
    public String adddistrict(@Valid Air air, @RequestParam(name="queryDistrict",required = false) String queryDistrict){
        Integer id = Integer.parseInt(queryDistrict);
        air.setDistrictId(id);
        air.setLastModifyTime(new Date());
        int count = airservice.addAir(air);
        return "redirect:/district";
    }
    @RequestMapping(value = "/changeair/{districtId}/{id}")
    public String changeair(Model model,@PathVariable Integer districtId,@PathVariable Integer id){
        List<District> districtList = districtservice.getDistrict();
        model.addAttribute("districtList",districtList);
        model.addAttribute("districtId",districtId);
        model.addAttribute("id",id);
        return "changeair";
    }
    @RequestMapping("/changeairs")
    public String changeairs(@Valid Air air,@RequestParam(name="id",required = false) String id,@RequestParam(name="queryDistrict",required = false) String queryDistrict){
        Integer qid = Integer.parseInt(queryDistrict);
        Integer cid = Integer.parseInt(id);
        air.setId(cid);
        air.setDistrictId(qid);
        air.setLastModifyTime(new Date());
        int count = airservice.chair(air);
        return "redirect:/district";
    }
    @RequestMapping("/del")
    public String del(@RequestParam(name = "delid") Integer delid){
        try {
            int count = airservice.del(delid);
        }catch (Exception e){
            throw new RuntimeException("删除失败!");
        }
        return "redirect:/district";
    }
}
